package bpm.service.impl.foreignactive;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import bpm.service.fs.FileService;
import bpm.dao.foreignactive.ForeignActiveAttachDao;
import bpm.domain.foreignactive.ForeignActiveAttach;
import bpm.service.foreignactive.ForeignActiveAttachService;
import bpm.util.CommonUUIDGenerator;

@Controller("foreignActiveAttachService")
@Transactional
public class ForeignActiveAttachServiceBean implements
		ForeignActiveAttachService {
	@Resource
	private ForeignActiveAttachDao foreignActiveAttachDao;
	private static final Logger logger=Logger.getLogger(ForeignActiveAttachServiceBean.class); 
	@Resource
	private FileService fileService;

	public void deleteForeignActiveAttach(ForeignActiveAttach foreignactiveattach) {
		String id = foreignactiveattach.getId();
		String resid = foreignactiveattach.getAtFileid();
		String sql = BuildFaSql.getDeleteAttachSql(id);
		logger.debug("DeleteSql "+sql);
		this.foreignActiveAttachDao.deleteForeignActiveAttach(sql);
		fileService.delete(resid);		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ForeignActiveAttach> findForeignActiveAttach(String id) {
		String sql = BuildFaSql.getfindForeignActiveAttachSql(id);
		logger.debug("QuerySql "+sql);
		return this.foreignActiveAttachDao.findForeignActiveAttachList(sql);		
	}

	public String saveForeignActiveAttach(ForeignActiveAttach foreignactiveattach) {
		String id=CommonUUIDGenerator.getID("AT");
		foreignactiveattach.setId(id);
		String sql = BuildFaSql.getAddAttachSql();
		Object[] temp = BuildFaSql.getAddAttachParams(foreignactiveattach);
		logger.debug("InsertSql "+sql);
		logger.debug("Params "+getString(temp));
		this.foreignActiveAttachDao.saveForeignActiveAttach(sql,temp);
		return id;		
	}

    public String getString(Object[] temp){
		
		StringBuffer temps=new StringBuffer();
		for(Object o:temp){
			temps.append(o+",");
		}
		return temps.toString();
	}
	
	
}
